#include <gtk/gtk.h>
#include <stdio.h>

#include "gtkutils.h"
#include "cjsonutils.h"

#include "logs/log.h"
#include "gateway/network-panel-wan.h"

void sayHello() {
    printf("hello word\n");
}

#define createObjType(obj, type) type *obj;
typedef void(*createLabelObj);

createObjType(model, GtkLabel);
createObjType(type, GtkLabel);
createObjType(hardversion, GtkLabel);
createObjType(sn, GtkLabel);

createObjType(firmware, GtkLabel);
createObjType(kernel, GtkLabel);
createObjType(architecture, GtkLabel);

createObjType(runtime, GtkLabel);
createObjType(systime, GtkLabel);
createObjType(load, GtkLabel);

createObjType(cnt, GtkLabel);

guint timer;
long count = 0;

static void settings() {
    char *str = "{\"type\":\"wiredconfig\",\"config\":{\"work\":\"static\",\"properties\":{\"address\":\"192.168.200.223\",\"netmask\":\"255.255.255.0\",\"gateway\":\"192.168.200.1\",\"dns1\":\"192.168.200.1\",\"dns2\":\"114.114.114.114\"}}}";
    cJson json = NULL;

    if ((json = toJsonObject(str))) {
        cJson config = NULL;
        if((config = getObjectItem(json, "config")) != NULL) {
            cJson work = NULL;
            if ((work = getObjectItem(config, "work"))) {
                INFO_LOG("work: (%s)", work->valuestring);
                if (LineEqual(work->valuestring, NET_IFACE_ATTR_WORK_DHCP)) {
                    WiredSetDynamic();
                }

                if (LineEqual(work->valuestring, NET_IFACE_ATTR_WORK_STATIC)) {
                    cJson properties = NULL;
                    cJson address = NULL;
                    cJson netmask = NULL;
                    cJson gateway = NULL;
                    cJson dns1 = NULL;
                    cJson dns2 = NULL;

                    WiredSetStatic();
                    if ((properties = getObjectItem(config, "properties"))) {
                        if ((address = getObjectItem(properties, "address"))) {
                            INFO_LOG("address: (%s)", address->valuestring);
                            WiredSetAddress(address->valuestring);
                        }
                        if ((netmask = getObjectItem(properties, "netmask"))) {
                            INFO_LOG("netmask: (%s)", netmask->valuestring);
                            WiredSetNetmaskAddress(netmask->valuestring);
                        }
                        if ((gateway = getObjectItem(properties, "gateway"))) {
                            INFO_LOG("gateway: (%s)", gateway->valuestring);
                            WiredSetGatewayAddress(gateway->valuestring);
                        }

                        GSList *DnsAddress = NULL;
                        if ((dns1 = getObjectItem(properties, "dns1"))) {
                            DnsAddress = g_slist_append(DnsAddress, dns1->valuestring);
                        }
                        if ((dns2 = getObjectItem(properties, "dns2"))) {
                            DnsAddress = g_slist_append(DnsAddress, dns2->valuestring);
                        }
                        char dnsvalue[100] = {0};
                        if (splitToStringWithSeparator(DnsAddress, ",", dnsvalue)) {
                            INFO_LOG("dns values: %s", dnsvalue);
                            WiredSetDnsAddress(dnsvalue);
                        }
                        GS_FREE(DnsAddress);
                    }
                }
            }
        }
        deleteJsonObject(json);
    }
}

createTimerFunc(update_info) {


   settings();
   
    char buffer[100] ={0};
    sprintf(buffer, "查询次数：%ld", count++);
    gtk_label_set_text(cnt, buffer);
    

    return true;
}

createSignalFunc(timer_start) {
    if(!timerStarted(&timer))
        timer = createTimerMilliSecond(15, update_info, NULL);
}
createSignalFunc(timer_stop) {
    stopTimer(&timer);
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;

    cnt = createLabel("查询次数：0");

    window = createWindowWithTitleSize(app, "gtk-timeout-system-info-test", 200, 100);
    
    GtkBox *timer_ctl_box = createBoxWithHorizntalAppends(5, 
        createButtonAction(NULL, "Start",timer_start),
        createButtonAction(NULL, "Stop",timer_stop),
        cnt,
        NULL
    );

    setBoxHorizntalAlign(timer_ctl_box, ACENTER);

    GtkBox *mainbox = createBoxWithVerticalAppends(5, 
        timer_ctl_box,
        NULL
    );
    setBoxFullMargin(mainbox, 30);

    setWindowChild(window, mainbox);
    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.systeminfo", 
                                G_APPLICATION_FLAGS_NONE, activate);
    // app = gtk_application_new("gtk.example.template", G_APPLICATION_FLAGS_NONE);
    // g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    // status = g_application_run(G_APPLICATION(app), argc, argv);
    // g_object_unref(app);
    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}

